<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\ImportLogRepository;
use App\Utils\Output;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;

/**
 * 控制器
 * Class ImportLogController
 * @package App\Http\Controllers\Admin
 */
class ImportLogController extends BaseController
{
    private $rep;

    public function __construct(ImportLogRepository $rep)
    {
        $this->set('import');
        $this->rep = $rep;
    }

    function index(Request $request, $type)
    {
        $search = $request->all();
        $search['type'] = $type;
        if ($request->ajax()) {
            return $this->rep->adminPager($search);
        }
        return $this->view('index');
    }

    function down($type, $id)
    {
        $entity = $this->rep->get($id);
        if ($type == 1) {
            return Response::download(Storage::path($entity->import_path), $entity->file_name);
        } else {
            return Response::download(Storage::path($entity->error_path), '错误数据.xlsx');
        }
    }

    function setSelectIds(Request $request)
    {
        $ids = $request->get('ids');
        $key = uniqid('id_key', true);
        Cache::put($key, $ids, 20);
        return Output::data($key);
    }
}
